package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.entity.wms.ClassifyEntity;
import com.group.entity.wms.GoodsEntity;
import com.group.entity.wms.PositionEntity;
import com.group.exception.IndustryException;
import com.group.mapper.ClassifyMapper;
import com.group.service.ClassifyService;
import com.group.service.GoodsService;
import com.group.service.PositionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, ClassifyEntity> implements ClassifyService {
    @Resource
    private GoodsService goodsService;
    @Resource
    private PositionService positionService;
    @Override
    public List<ClassifyEntity> findAll() {
        List<ClassifyEntity> classifyEntities = this.baseMapper.selectList(null);
        return classifyEntities;
    }

    @Override
    public Integer add(ClassifyEntity classifyEntity) {

        classifyEntity.setCreateUser(1024L);
        classifyEntity.setCreateTime(new Date());

        boolean save = this.save(classifyEntity);
        return save?1:0;
    }

    @Override
    public Integer update(ClassifyEntity classifyEntity) {

        int i = this.baseMapper.updateById(classifyEntity);
        return i;
    }

    @Override
    public Integer delete(Long id) {
        //查看该分类信息是否绑定储位，是否绑定商品
        List<GoodsEntity> goodsEntityList = this.goodsService.getBaseMapper().selectList(new QueryWrapper<GoodsEntity>().eq("goods_type", id));

        if (goodsEntityList!=null&&goodsEntityList.size()>=1) throw new IndustryException("该分类下存在商品信息无法删除");

        //是否绑定储位信息
        List<PositionEntity> positionEntities = this.positionService.getBaseMapper().selectList(new QueryWrapper<PositionEntity>().eq("position_type", id));

        if (positionEntities!=null&&positionEntities.size()>=1) throw new IndustryException("还有存储该商品类型的储位，无法删除");

        return this.baseMapper.deleteById(id);
    }

    @Override
    public List<ClassifyEntity> findByNameLike(String keyWorld) {
        List<ClassifyEntity> list = this.baseMapper.selectList(new QueryWrapper<ClassifyEntity>().like("classify_name", keyWorld));
        return list;
    }
}
